//
//  AppDelegate.m
//  weibo
//
//  Created by hp on 16/4/21.
//  Copyright © 2016年 hp. All rights reserved.
//

#import "AppDelegate.h"

#import "MainTabBarController.h"
#import "OAuthViewController.h"
#import "NewFeatureViewController.h"
#import "UserAccount.h"
#import "WelcomeViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 创建窗体
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    // 窗体可见
    [self.window makeKeyAndVisible];

    self.window.rootViewController = [self defaultViewController];
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swithRootViewController:) name:kNotificationSwitchRootViewController object:nil];
    [self customApperance];
    
    return YES;
}
// 自定义风格
- (void)customApperance
{
    [UINavigationBar appearance].tintColor = [UIColor orangeColor];
    [UITabBar appearance].tintColor = [UIColor orangeColor];
}

- (void)swithRootViewController:(NSNotification *)n
{
    NSDictionary *dict = n.userInfo;
    if ([dict[@"VC"] isEqualToString:@"Main"]) {
        
        self.window.rootViewController = [[MainTabBarController alloc] init];
    } else if([dict[@"VC"] isEqualToString:@"Welcome"]){
        
        self.window.rootViewController = [[WelcomeViewController alloc] init];
    }else {
        self.window.rootViewController = [self defaultViewController];
    }
}

- (UIViewController *)defaultViewController
{
    if ([self isNewFeature]) {
        return [[NewFeatureViewController alloc] initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init] ];
    } else {
        // 判断有没有登陆
        if ([[UserAccount sharedUserAccount] isLogined]) {
            
            // 显示欢迎页
            return [[WelcomeViewController alloc] init];
        } else {
            // 没登陆过就直接显示主MainTabBarController
            return [[MainTabBarController alloc] init];
        }
    }
}

- (BOOL)isNewFeature
{
    NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
    NSString *currentVersion = infoDict[@"CFBundleShortVersionString"];
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFBundleShortVersionString"];
    if (lastVersion!=nil&&[lastVersion isEqualToString:currentVersion]) {
        return NO;
    } else {
        [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:@"CFBundleShortVersionString"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return YES;
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
